﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;

namespace SELF.Controls
{
    public partial class PlayerControl : DevExpress.XtraEditors.XtraForm
    {
        public PlayerControl()
        {
            InitializeComponent();
            
            
        }
        private int screenHeight = 0;
        private int screenWidth = 0;

        public int ScreenWidth
        {
            get { return screenWidth; }
            set { screenWidth = value; }
        }

        public int ScreenHeight
        {
            get { return screenHeight; }
            set { screenHeight = value; }
        }
        System.Windows.Forms.Timer tmRead = new System.Windows.Forms.Timer();
        private void PlayerControl_Load(object sender, EventArgs e)
        {
            this.Location = new Point(0, ScreenHeight - 115);
            this.Size = new Size(ScreenWidth, 115);
            this.Height = 115;
            tmRead.Interval = 1000;
            tmRead.Start();
            tmRead.Tick += new EventHandler(tmRead_Tick);
            radioButton2.Checked = true;
        }

        void tmRead_Tick(object sender, EventArgs e)
        {
            gTrackBar1.MaxValue = Convert.ToInt32(Math.Round(VDOStatus.totalLen));
            gTrackBar1.Value = Convert.ToInt32(Math.Round(VDOStatus.currentLen));
            //throw new NotImplementedException();
            labelControl2.Text = SELF.VDOStatus.txtCurrentLen+"/"+SELF.VDOStatus.txtTotalLen;
            if ((SELF.VDOStatus.currentLen == SELF.VDOStatus.totalLen)&&(SELF.VDOStatus.currentLen>0))
            {
                tmRead.Stop();
                this.Close();
                this.Dispose();
            }
        }
        #region seeking
        private void gTrackBar1_MouseDown(object sender, MouseEventArgs e)
        {
            SELF.VDOStatus.seeking = true;
            SELF.VDOStatus.unSeeking = false;
            tmRead.Stop();
        }
        //when click and drag.
        
        private void gTrackBar1_MouseMove(object sender, MouseEventArgs e)
        {
            if (SELF.VDOStatus.seeking)
            {
                SELF.VDOStatus.idleTime = 0;
                SELF.VDOStatus.seekLen = gTrackBar1.Value;
            }
        }

        private void gTrackBar1_MouseUp(object sender, MouseEventArgs e)
        {
            SELF.VDOStatus.unSeeking = true;
            if (SELF.VDOStatus.seeking)
            {
                SELF.VDOStatus.seeking = false;
                SELF.VDOStatus.seekBuffer = 1;
            }
            tmRead.Start();
        }
        #endregion
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                SELF.VDOStatus._WMPRate = 1;
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
            {
                SELF.VDOStatus._WMPRate = 1.4;
            }
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton4.Checked)
            {
                SELF.VDOStatus._WMPRate = 1.8;
            }
        }

        private void btStop_Click(object sender, EventArgs e)
        {
            SELF.VDOStatus.playerStatus = VDOStatus.PlayerStatus.Stop;
        }
        int pause = 0;
        private void btPause_Click(object sender, EventArgs e)
        {
            if (pause==0)
            {
                SELF.VDOStatus.playerStatus = VDOStatus.PlayerStatus.Pause;
                pause = 1;
                btPause.Image = global::SELF.Properties.Resources.play;
            }else
                if (pause == 1)
                {
                    SELF.VDOStatus.playerStatus = VDOStatus.PlayerStatus.Play;
                    pause = 0;
                    btPause.Image = global::SELF.Properties.Resources.Pause;
                }
        }

        private void PlayerControl_FormClosing(object sender, FormClosingEventArgs e)
        {
            SELF.Model.Leaning.SetMechineState(SELF.Model.Leaning.StateMechine.Logon);
            tmRead.Stop();
        }

        private void btBack_Click(object sender, EventArgs e)
        {
            SELF.VDOStatus.idleTime = 0;
            double len = SELF.VDOStatus.currentLen;
            SELF.VDOStatus.seekLen = (len - 10.0)<0?0:len-10;
            SELF.VDOStatus.seekBuffer = 1;
        }

        private void btNext_Click(object sender, EventArgs e)
        {
            SELF.VDOStatus.idleTime = 0;
            double len = SELF.VDOStatus.currentLen;
            SELF.VDOStatus.seekLen = (len + 10.0) > SELF.VDOStatus.totalLen ? SELF.VDOStatus.totalLen - 1 : len + 10;
            SELF.VDOStatus.seekBuffer = 1;
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            VDOStatus.volumn = gTrackBar2.Value;
        }
    }
}